#!/usr/bin/env python
import os

class CFGFile ( object ):
        def __init__ ( self, fname = '', fail = False ):
                self._data = {}
                self._old_filename = ''

		try:
                	self.read ( fname )
		except:
			if fail: 
				raise

        def read ( self, fname = '' ):
                if not fname:
                        fname = self._old_filename


		if not fname: return
		if not os.path.isfile ( fname ): return

                self._old_filename = fname

                for l in open ( fname ):
                        l = l.strip ()
                        if not l or l [ 0 ] == '#':
                                continue

                        l = l.split ( "=", 1 )
                        self._data [ l [ 0 ].lower () ] = l [ 1 ]

        def get ( self, name, defval = None ):
		name = str ( name )
                name = name.lower ()
                return self._data.get ( name, defval )

	def set ( self, name, val ):
		name = name.lower ()
		self._data [ name ] = val

        def __getitem__ ( self, name ):
                return self.get ( name )

	def __setitem__ ( self, name, val ):
		return self.set ( name, val )

	def __str__ ( self ):
		return str ( self._data )

if __name__ == '__main__':
	a = CFGFile ()
	a [ 'ciao' ] = 'mamma'

	print a [ 'ciao' ]
	
